<HTML>
<HEAD>
<TITLE>JavaScript Source Code 3000:  Calculators:  Pregnancy Calculator</TITLE>
<META HTTP-EQUIV="JavaScript Source Code 3000" CONTENT = "no-cache">
<META NAME="date" CONTENT="2000-09-09">
<META NAME="channel" CONTENT="Web Developer">
<META NAME="author" CONTENT="Ronnie T. Moore, Editor">
<META NAME="section" CONTENT="Calculators">
<META NAME="description" CONTENT="Enter the mother's pregnancy information into this calculator and JavaScript will provide an estimated conception date, birth due date, and an estimated current fetal age.   Great for all those mothers-to-be!">

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Ronnie T. Moore, Editor -->
<!-- Web Site:  JavaScript Source Code 3000 -->




<!-- Begin
function isValidDate(dateStr) {
// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}

function dispDate(dateObj) {
month = dateObj.getMonth()+1;
month = (month < 10) ? "0" + month : month;

day   = dateObj.getDate();
day = (day < 10) ? "0" + day : day;

year  = dateObj.getYear();
if (year < 2000) year += 1900;

return (month + "/" + day + "/" + year);
}

function pregnancyCalc(pregform) {
menstrual = new Date(); // creates new date objects
ovulation = new Date();
duedate = new Date();
today = new Date();
cycle = 0, luteal = 0; // sets variables to invalid state ==> 0

if (isValidDate(pregform.menstrual.value)) { // Validates menstual date 
menstrualinput = new Date(pregform.menstrual.value);
menstrual.setTime(menstrualinput.getTime())
}
else return false; // otherwise exits

cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value); // defaults to 28
// validates cycle range, from 22 to 45
if (pregform.cycle.value != "" && (pregform.cycle.value < 22 || pregform.cycle.value > 45)) {
alert("Your cycle length is either too short or too long for \n"
+ "calculations to be very accurate!  We will still try to \n"
+ "complete the calculation with the figure you entered. ");
}

luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value); // defaults to 14
// validates luteal range, from 9 to 16
if (pregform.luteal.value != "" && (pregform.luteal.value < 9 || pregform.luteal.value > 16)) {
alert("Your luteal phase length is either too short or too long for \n"
+ "calculations to be very accurate!  We will still try to complete \n"
+ "the calculation with the figure you entered. ");
}

// sets ovulation date to menstrual date + cycle days - luteal days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
pregform.conception.value = dispDate(ovulation);

// sets due date to ovulation date plus 266 days
duedate.setTime(ovulation.getTime() + 266*86400000);
pregform.duedate.value = dispDate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
var fetalage = 14 + 266 - ((duedate - today) / 86400000);
weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
fetalage = weeks + " week" + (weeks > 1 ? "s" : "") + ", " + days + " days";
pregform.fetalage.value = fetalage;

return false; // form should never submit, returns false
}
//  End -->
</script>
</HEAD>

<BODY BGCOLOR=#ffffff vlink=#0000ff>

<BR>
<center>
<table width=600 cellpadding=0 cellspacing=10>
<tr>
<td width=468 align=center>

    
    
</td>
<td width=120 align=center>
    
</td>
</tr>
</table>
<BR>
<BR>
<basefont size=3>
<FONT SIZE="+2" FACE="Helvetica,Arial">
<A HREF="../index.htm" /" TARGET="_top"><FONT COLOR="#0000FF"><b>Home</b></font></A>
<img src="../img/arrow.gif" /img/arrow.gif" height=13 width=7 border=0 alt="}">
<A HREF="index.htm" /calculators/"><font color="#FF0000"><b>Calculators</b></font></A>
<img src="../img/arrow.gif" /img/arrow.gif" height=13 width=7 border=0 alt="}">
<FONT COLOR="#006666"><b>Pregnancy Calculator</b></font></font>
<BR>
<BR>
<table BORDER=0 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr><td><font FACE="helvetica,arial,geneva">
<br>
<br>
<!-- Description --><!--content_start-->
Enter the mother's pregnancy information into this calculator and JavaScript will provide an estimated conception date, birth due date, and an estimated current fetal age.   Great for all those mothers-to-be!
<hr>
</td></tr>
</table>
<!-- Demonstration -->
<center>
<form onSubmit="return pregnancyCalc(this);">
<table>
<tr><td>
<pre>
Last Menstrual Period:        <input type=text name=menstrual value="" size=10 maxlength=10>
                              (MM/DD/YYYY format)

Average Length of Cycles:     <input type=text name=cycle value="" size=3 maxlength=3> (22 to 45)
                              (defaults to 28)

Average Luteal Phase Length:  <input type=text name=luteal value="" size=3 maxlength=3> (9 to 16)
                              (defaults to 14)

<center><input type=submit value="Calculate!"></center>

Estimated Conception:         <input type=text name=conception value="" size=20>
Estimated Due Date:           <input type=text name=duedate value="" size=20>
Estimated Fetal Age:          <input type=text name=fetalage value="" size=20>
</pre>
</td></tr>
</table>
</form>
</center>

<P>
<P>
<a name="source">
<table BORDER=0 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr><td BGCOLOR=yellow><font FACE="helvetica,arial,geneva"><b>JavaScript Source Code 3000:  Calculators:  Pregnancy Calculator</b>
<p>Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Apple-s).  The script is yours!!!
<br><br></font></td></tr>
<tr><td BGCOLOR=yellow ALIGN=CENTER>
<form NAME="copy">

<DIV align="center">
<input type=button value="Highlight All" onClick="javascript:this.form.txt.focus();this.form.txt.select();">&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="text" NAME="total" VALUE="Script Size:  5.17 KB" size=24>
</DIV>

<textarea NAME="txt" ROWS=20 COLS=75 WRAP=VIRTUAL>

&lt;!-- TWO STEPS TO INSTALL PREGNANCY CALCULATOR:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  --&gt;

&lt;!-- STEP ONE: Paste this code into the HEAD of your HTML document  --&gt;

&lt;HEAD&gt;

&lt;SCRIPT LANGUAGE="JavaScript"&gt;
&lt;!-- Original:  Ronnie T. Moore, Editor --&gt;
&lt;!-- Web Site:  JavaScript Source Code 3000 --&gt;

&lt;! &gt;
&lt;! &gt;

&lt;!-- Begin
function isValidDate(dateStr) {
// Date validation function courtesty of 
// Sandeep V. Tamhankar (stamhankar@hotmail.com) --&gt;

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month &lt; 1 || month &gt; 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day &lt; 1 || day &gt; 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day&gt;29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}

function dispDate(dateObj) {
month = dateObj.getMonth()+1;
month = (month &lt; 10) ? "0" + month : month;

day   = dateObj.getDate();
day = (day &lt; 10) ? "0" + day : day;

year  = dateObj.getYear();
if (year &lt; 2000) year += 1900;

return (month + "/" + day + "/" + year);
}

function pregnancyCalc(pregform) {
menstrual = new Date(); // creates new date objects
ovulation = new Date();
duedate = new Date();
today = new Date();
cycle = 0, luteal = 0; // sets variables to invalid state ==&gt; 0

if (isValidDate(pregform.menstrual.value)) { // Validates menstual date 
menstrualinput = new Date(pregform.menstrual.value);
menstrual.setTime(menstrualinput.getTime())
}
else return false; // otherwise exits

cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value); // defaults to 28
// validates cycle range, from 22 to 45
if (pregform.cycle.value != "" && (pregform.cycle.value &lt; 22 || pregform.cycle.value &gt; 45)) {
alert("Your cycle length is either too short or too long for \n"
+ "calculations to be very accurate!  We will still try to \n"
+ "complete the calculation with the figure you entered. ");
}

luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value); // defaults to 14
// validates luteal range, from 9 to 16
if (pregform.luteal.value != "" && (pregform.luteal.value &lt; 9 || pregform.luteal.value &gt; 16)) {
alert("Your luteal phase length is either too short or too long for \n"
+ "calculations to be very accurate!  We will still try to complete \n"
+ "the calculation with the figure you entered. ");
}

// sets ovulation date to menstrual date + cycle days - luteal days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
pregform.conception.value = dispDate(ovulation);

// sets due date to ovulation date plus 266 days
duedate.setTime(ovulation.getTime() + 266*86400000);
pregform.duedate.value = dispDate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
var fetalage = 14 + 266 - ((duedate - today) / 86400000);
weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
fetalage = weeks + " week" + (weeks &gt; 1 ? "s" : "") + ", " + days + " days";
pregform.fetalage.value = fetalage;

return false; // form should never submit, returns false
}
//  End --&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document  --&gt;

&lt;BODY&gt;

&lt;center&gt;
&lt;form onSubmit="return pregnancyCalc(this);"&gt;
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;
&lt;pre&gt;
Last Menstrual Period:        &lt;input type=text name=menstrual value="" size=10 maxlength=10&gt;
                              (MM/DD/YYYY format)

Average Length of Cycles:     &lt;input type=text name=cycle value="" size=3 maxlength=3&gt; (22 to 45)
                              (defaults to 28)

Average Luteal Phase Length:  &lt;input type=text name=luteal value="" size=3 maxlength=3&gt; (9 to 16)
                              (defaults to 14)

&lt;center&gt;&lt;input type=submit value="Calculate!"&gt;&lt;/center&gt;

Estimated Conception:         &lt;input type=text name=conception value="" size=20&gt;
Estimated Due Date:           &lt;input type=text name=duedate value="" size=20&gt;
Estimated Fetal Age:          &lt;input type=text name=fetalage value="" size=20&gt;
&lt;/pre&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/center&gt;


 

&lt;!-- Script Size:  5.17 KB --&gt;</textarea><br><font FACE="helvetica,arial,geneva"></font></td></tr>

</table>
</form>
</FONT>
</CENTER>


</center>
</body></html>
